﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RealtorsPortal.Entities;
using System.Data.SqlClient;
using System.Data;

namespace RealtorsPortal.Data_Access_Layer
{
    public class NewsDAL
    {
        DataAccessHelper daAccHelp = new DataAccessHelper();

        public DataTable getNewsData()
        {
            DataTable dt = new DataTable();
            dt = daAccHelp.GetDataTable("sp_Get_News");
            return dt;
        }
        /// <summary>
        /// insert News into database using procedure
        /// </summary>
        /// <param name="nw">News object to insert</param>
        /// <returns>true if success, otherwise return false</returns>
        public bool InsertNews(News nw)
        {
            List<SqlParameter> lstParams = new List<SqlParameter>();
            SqlParameter param;
            ///
            ///
            ///
            //get each nwtory properties and add to params list
            ///
            ///
            ///
            param = new SqlParameter("@AdminId", SqlDbType.Int);
            param.Value = nw.UserId;
            lstParams.Add(param);
            ///
            param = new SqlParameter("@Title", SqlDbType.VarChar);
            param.Value = nw.Title;
            lstParams.Add(param);
            ///
            param = new SqlParameter("@Content", SqlDbType.Text);
            param.Value = nw.Content;
            lstParams.Add(param);
            ///
            param = new SqlParameter("@NewsImage", SqlDbType.VarChar);
            param.Value = nw.NewsImage;
            lstParams.Add(param);
            ///
            param = new SqlParameter("@WritedDate", SqlDbType.VarChar);
            param.Value = nw.WritedDate;
            lstParams.Add(param);
            ///

            //Run Procedure

            int returnValue = daAccHelp.ExcuteStoredNonQuery("sp_addNews", lstParams);

            //return value the indinw that the inserting was sussesed or not
            if (returnValue > 0) { return true; }
            else { return false; }
        }

        /// <summary>
        /// Update News from DB method
        /// </summary>
        /// <param name="nw">new News to update</param>
        /// <param name="oldNewsId">the ID of the News which we want to change</param>
        /// <returns>true if success, otherwise return false</returns>
        public bool UpdateNews(News nw)
        {
            List<SqlParameter> lstParams = new List<SqlParameter>();
            SqlParameter param;
            ///
            ///
            ///
            //get each nwtory properties and add to params list
            ///
            param = new SqlParameter("@OldNewsId", SqlDbType.VarChar);
            param.Value = nw.NewsId;
            lstParams.Add(param);
            ///
            /// 
            ///
            param = new SqlParameter("@Title", SqlDbType.VarChar);
            param.Value = nw.Title;
            lstParams.Add(param);
            ///
            param = new SqlParameter("@Content", SqlDbType.Text);
            param.Value = nw.Content;
            lstParams.Add(param);
            ///
            param = new SqlParameter("@NewsImage", SqlDbType.VarChar);
            param.Value = nw.NewsImage;
            lstParams.Add(param);
            ///
            param = new SqlParameter("@WritedDate", SqlDbType.VarChar);
            param.Value = nw.WritedDate;
            lstParams.Add(param);
            ///

            //Run Procedure

            int returnValue = daAccHelp.ExcuteStoredNonQuery("sp_updateNews", lstParams);

            //return value the indinw that the inserting was sussesed or not
            if (returnValue > 0) { return true; }
            else { return false; }
        }


        /// <summary>
        /// Delete News from DB method
        /// </summary>
        /// <param name="NewsId"> the nw id to delete</param>
        /// <returns>true if success, otherwise return false</returns>

        public bool DeleteNews(int NewsId)
        {
            List<SqlParameter> lstParams = new List<SqlParameter>();
            SqlParameter param;
            ///
            param = new SqlParameter("@NewsId", SqlDbType.Int);
            param.Value = NewsId;
            lstParams.Add(param);
            ///

            //Run Procedure

            int returnValue = daAccHelp.ExcuteStoredNonQuery("sp_deleteNews", lstParams);

            //return value the indinw that the Deleting was sussesed or not
            if (returnValue > 0) { return true; }
            else { return false; }
        }
    }
}